/**
 *
 */
package client;

import messaage.AddForumMsg;
import messaage.AddFriendMsg;
import messaage.AddPostMsg;
import messaage.CreateThreadMsg;
import messaage.DisconnectMsg;
import messaage.GetForumTopicsMsg;
import messaage.GetListOfForumsMsg;
import messaage.LoginMsg;
import messaage.LogoutMsg;
import messaage.Message;
import messaage.RegisterMsg;
import messaage.RemoveFriendMsg;
import messaage.GetThreadMsg;

/**
 * @author Alon Segal
 *
 */
public class MessageBuilder {


	public MessageBuilder() {
		super();
	}

	public Message inputToMessage(String keyIn) {
		Message ans = null;

		if (keyIn.length() >= 6 && keyIn.substring(0, 5).equals("login")) {
			String tmpS = keyIn.substring(6);
			String[] lInfo = tmpS.split(" ");
			if (lInfo.length != 2) {
				System.out.println("bad login input");
				return null;
			}
			ans = new LoginMsg(lInfo[0], lInfo[1]);
		}

		if (keyIn.length() >= 6 && keyIn.substring(0, 6).equals("logout")) {
			ans = new LogoutMsg("");
		}

		if (keyIn.length() >= 9 && keyIn.substring(0, 8).equals("register")) {
			String tmpS = keyIn.substring(9);
			String[] lInfo = tmpS.split(" ");
			if (lInfo.length != 6) {
				System.out.println("bad register input");
				return null;
			}
			ans = new RegisterMsg(lInfo[0], lInfo[1], lInfo[2], lInfo[3].charAt(0), lInfo[4], lInfo[5]);
		}

		if (keyIn.length() >= 10 && keyIn.substring(0, 10).equals("disconnect")) {
			ans = new DisconnectMsg("");
		}

		if (keyIn.length() >= 10 && keyIn.substring(0, 9).equals("addfriend")) {
			String tmpS = keyIn.substring(10);
			if (tmpS.equals("")) {
				return null;
			}
			ans = new AddFriendMsg("", tmpS);
		}
		if (keyIn.length() >= 13 && keyIn.substring(0, 12).equals("removefriend")) {
			String tmpS = keyIn.substring(13);
			if (tmpS.equals("")) {
				return null;
			}
			ans = new RemoveFriendMsg("", tmpS);
		}
		if (keyIn.length() >= 9 && keyIn.substring(0, 9).equals("getforums")) {
			ans = new GetListOfForumsMsg("");
		}
		if (keyIn.length() >= 9 && keyIn.substring(0, 8).equals("addforum")) {
			String tmpS = keyIn.substring(9);
			if (tmpS.equals("")) {
				return null;
			}
			ans = new AddForumMsg("", tmpS);
		}

		if (keyIn.length() >= 10 && keyIn.substring(0, 9).equals("gettopics")) {
			String tmpS = keyIn.substring(10);
			if (tmpS.equals("")) {
				System.out.println("bad gettopics input");
				return null;
			}
			ans = new GetForumTopicsMsg("", tmpS);
		}

		if (keyIn.length() >= 13 && keyIn.substring(0, 12).equals("createthread")) {
			String tmpS = keyIn.substring(13);
			String[] lInfo = tmpS.split(" ");
			if (lInfo.length != 3) {
				System.out.println("bad createthread input");
				return null;
			}
			ans = new CreateThreadMsg("", lInfo[0], lInfo[1], lInfo[2]);
		}

		if (keyIn.length() >= 8 && keyIn.substring(0, 7).equals("addpost")) {
			String tmpS = keyIn.substring(8);
			String[] lInfo = tmpS.split(" ");
			if (lInfo.length != 5) {
				System.out.println("bad addpost input");
				return null;
			}
			ans = new AddPostMsg("", lInfo[0], lInfo[1], lInfo[2],lInfo[3]);
		}

		if (keyIn.length() >= 10 && keyIn.substring(0, 9).equals("getthread")) {
			String tmpS = keyIn.substring(10);
			String[] lInfo = tmpS.split(" ");
			if (lInfo.length != 2) {
				System.out.println("bad getthread input");
				return null;
			}
			ans = new GetThreadMsg("", lInfo[0], lInfo[1]);
		}

		return ans;
	}
}
